﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace com.kmer.Helper {
	public static class DataHelper {
		/// <summary>
		/// 从DataTable读取数据，读取第一行
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="pData"></param>
		/// <param name="pName"></param>
		/// <returns></returns>
		public static T Get<T>(this DataSet pData, string pName) {
			return DataHelper.Get<T>(pData, 0, pName);
		}
		/// <summary>
		/// 从DataTable读取数据，读取第一行
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="pData"></param>
		/// <param name="pTableIndex"></param>
		/// <param name="pName"></param>
		/// <returns></returns>
		public static T Get<T>(this DataSet pData, int pTableIndex, string pName) {
			if (pData.Tables.Count > pTableIndex && pTableIndex >= 0) {
				return DataHelper.Get<T>(pData.Tables[pTableIndex], pName);
			} else {
				return default(T);
			}
		}
		/// <summary>
		/// 从DataTable读取数据，读取第一行
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="table"></param>
		/// <param name="pName">要读取的列名称，如果无效 返回default(T)</param>
		/// <returns></returns>
		public static T Get<T>(this DataTable table, string pName) {
			return DataHelper.Get<T>(table, 0, pName);
		}
		/// <summary>
		/// 从DataTable读取数据
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="table"></param>
		/// <param name="rowIndex">行序，号从0开始，如果无效则 返回default(T)</param>
		/// <param name="pName">要读取的列名称，如果无效 返回default(T)</param>
		/// <returns></returns>
		public static T Get<T>(this DataTable table, int rowIndex, string pName) {
			if (table.Rows.Count <= rowIndex || rowIndex < 0 || table.Columns.Contains(pName) == false) {
				return default(T);
			} else {
				var row = table.Rows[rowIndex];
				if (row.IsNull(pName)) return default(T);
				return row.Field<T>(pName);
			}
		}
		public static bool IsNull(this DataTable table, string pName) {
			return DataHelper.IsNull(table, 0, pName);
		}
		public static bool IsNull(this DataTable table, int rowIndex, string pName) {
			if (table.Rows.Count <= rowIndex || rowIndex < 0 || table.Columns.Contains(pName) == false) {
				return true;
			} else {
				var row = table.Rows[rowIndex];
				return row.IsNull(pName);
			}
		}
		/// <summary>
		/// 从DataTable读取数据，读取第一行
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="table"></param>
		/// <param name="pName">要读取的列名称，如果无效 返回输入的默认值（dv）</param>
		/// <param name="dv"></param>
		/// <returns></returns>
		public static T Get<T>(this DataTable table, string pName, T dv) {
			return DataHelper.Get<T>(table, 0, pName, dv);
		}
		/// <summary>
		/// 从DataTable读取数据
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="table"></param>
		/// <param name="rowIndex">行序，号从0开始，如果无效则返回 输入的默认值（dv）</param>
		/// <param name="pName">要读取的列名称，如果无效 返回输入的默认值（dv）</param>
		/// <param name="dv"></param>
		/// <returns></returns>
		public static T Get<T>(this DataTable table, int rowIndex, string pName, T dv) {
			if (table.Rows.Count <= rowIndex || rowIndex < 0 || table.Columns.Contains(pName) == false) {
				return dv;
			} else {
				var row = table.Rows[rowIndex];
				if (row.IsNull(pName)) return dv;
				return row.Field<T>(pName);
			}
		}
		public static List<DataRow> GetRows(this DataView view) {
			return view.Cast<DataRowView>().Select(item => item.Row).ToList();
		}
		public static List<DataRow> GetRows(this DataTable table) {
			return table.Rows.Cast<DataRow>().ToList();
		}
		public static List<DataRow> GetRows(this DataSet pData, int pTableIndex) {
			var r = new List<DataRow>();
			if (pData.Tables.Count > pTableIndex && pTableIndex >= 0) {
				return pData.Tables[pTableIndex].Rows.Cast<DataRow>().ToList();
			}
			return r;
		}
	}
}
